package com.app.server.impl;

import cn.hutool.core.util.StrUtil;
import com.app.mapper.*;
import com.app.model.*;
import com.app.model.AnimalRescueModel;
import com.app.server.AnimalRescueService;
import com.app.utils.StringTool;
import com.app.vo.AnimalRescueVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class AnimalRescueServiceImpl extends ServiceImpl<AnimalRescueMapper, AnimalRescueModel> implements AnimalRescueService {
    @Autowired
    private AnimalRescueMapper animalRescueMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private AdminUserMapper adminUserMapper;

    @Autowired
    private RescueHandMapper rescueHandMapper;

    @Autowired
    private RecordFileMapper recordFileMapper;

    @Autowired
    private UnitsManagerMapper unitsManagerMapper;

    @Autowired
    private UnitsRescueMapper unitsRescueMapper;
    @Autowired
    private UserMapper userMapper;

    //分页查询
    @Override
    public AnimalRescueVo getAnimalRescueList(Integer page, Integer limit, Integer departmentId, Integer type) {
        Page<AnimalRescueModel> page1 = new Page<>(page, limit);
        QueryWrapper<AnimalRescueModel> qwRescue = new QueryWrapper<>();
        if (type != null) {
            qwRescue.eq("type", type);
        }
        if (departmentId != null) {
//            QueryWrapper<UnitsManagerModel> qw =new QueryWrapper<>();
            UnitsManagerModel unitsManager = unitsManagerMapper.selectById(departmentId);
            String name = unitsManager.getName();
            qwRescue.eq("real_name", name);
        }
        qwRescue.orderByDesc("id");
        StringBuilder url = new StringBuilder();
        StringBuilder VideoUrl = new StringBuilder();
        Page<AnimalRescueModel> animalRescuePage = animalRescueMapper.selectPage(page1, qwRescue);
        List<AnimalRescueModel> animalRescueList = animalRescuePage.getRecords();
        for (int i = 0; i < animalRescueList.size(); i++) {
            if (StrUtil.isNotBlank(animalRescueList.get(i).getFileId())) {
                String[] split = animalRescueList.get(i).getFileId().split(",");
                for (int i1 = 0; i1 < split.length; i1++) {
                    if (StrUtil.isNotBlank(split[i1])) {
                        RecordFileModel recordFileModel = recordFileMapper.selectById(split[i1]);
                        if (recordFileModel != null) {
                            if (recordFileModel.getFileType() != null && recordFileModel.getFileType() == 1) {
                                url.append(recordFileModel.getUrl()).append(",");
                            } else if (recordFileModel.getFileType() != null && recordFileModel.getFileType() == 2) {
                                VideoUrl.append(recordFileModel.getUrl()).append(",");
                            }
                        }
                    }
                }
            }
            animalRescueList.get(i).setUrl(url.toString());
            animalRescueList.get(i).setVideoUrl(VideoUrl.toString());
        }
        for (AnimalRescueModel animalRescue : animalRescueList) {
            Integer animalType = animalRescue.getType();
            QueryWrapper<CategoryModel> qw = new QueryWrapper<>();
            qw.eq("id", animalType);
            CategoryModel category = categoryMapper.selectOne(qw);
            animalRescue.setTypeText(category.getName());
            Integer allocationStatus = animalRescue.getAllocationStatus();
            if (allocationStatus == 1) {
                animalRescue.setStatusText("待分配");
            }
            if (allocationStatus == 2) {
                animalRescue.setStatusText("已分配");
            }
            if (allocationStatus == 3) {
                animalRescue.setStatusText("已办结");
            }

        }

        long count = animalRescuePage.getTotal();
        AnimalRescueVo animalRescueVo = new AnimalRescueVo();
        animalRescueVo.setList(animalRescueList);
        animalRescueVo.setCount((int) count);
        return animalRescueVo;
    }

    //根据当前登录的单位查询列表
    @Override
    public AnimalRescueVo getAnimalRescueByUnitsList(Integer page, Integer limit, Integer departmentId, Integer type, Integer unitsId) {
        Page<AnimalRescueModel> page1 = new Page<>(page, limit);
        QueryWrapper<AnimalRescueModel> qwRescue = new QueryWrapper<>();

        QueryWrapper<UnitsRescueModel> qwUnits = new QueryWrapper<>();
        if (unitsId != null) {
            qwUnits.eq("units_id", unitsId);
        }
        List<UnitsRescueModel> unitsRescueList = unitsRescueMapper.selectList(qwUnits);
        List<Integer> rescueIdList = new ArrayList<>();
        for (UnitsRescueModel unitsRescue : unitsRescueList) {
            Integer rescueId = unitsRescue.getRescueId();
            rescueIdList.add(rescueId);
        }

        if (rescueIdList.size() == 0) {
            AnimalRescueVo animalRescueVo = new AnimalRescueVo();
            animalRescueVo.setList(null);
            animalRescueVo.setCount(0);
            return animalRescueVo;
        }
        if (!rescueIdList.isEmpty()) {
            qwRescue.in("id", rescueIdList);
        }


        if (type != null) {
            qwRescue.eq("type", type);
        }
        if (departmentId != null) {
            UnitsManagerModel unitsManager = unitsManagerMapper.selectById(departmentId);
            if (unitsManager != null) {
                String name = unitsManager.getName();
                qwRescue.eq("real_name", name);
            }
        }
        qwRescue.orderByDesc("id");
        StringBuilder url = new StringBuilder();
        StringBuilder VideoUrl = new StringBuilder();
        Page<AnimalRescueModel> animalRescuePage = animalRescueMapper.selectPage(page1, qwRescue);
        List<AnimalRescueModel> animalRescueList = animalRescuePage.getRecords();
        for (int i = 0; i < animalRescueList.size(); i++) {
            if (StrUtil.isNotBlank(animalRescueList.get(i).getFileId())) {
                String[] split = animalRescueList.get(i).getFileId().split(",");
                for (int i1 = 0; i1 < split.length; i1++) {
                    RecordFileModel recordFileModel = recordFileMapper.selectById(split[i1]);
                    if (recordFileModel != null) {
                        if (recordFileModel.getFileType() != null && recordFileModel.getFileType() == 1) {
                            url.append(recordFileModel.getUrl()).append(",");
                        } else if (recordFileModel.getFileType() != null && recordFileModel.getFileType() == 2) {
                            VideoUrl.append(recordFileModel.getUrl()).append(",");
                        }
                    }
                }
            }
            animalRescueList.get(i).setOpenId(null);
            animalRescueList.get(i).setUrl(url.toString());
            animalRescueList.get(i).setVideoUrl(VideoUrl.toString());
        }
        for (AnimalRescueModel animalRescue : animalRescueList) {
            Integer animalType = animalRescue.getType();
            QueryWrapper<CategoryModel> qw = new QueryWrapper<>();
            qw.eq("id", animalType);
            CategoryModel category = categoryMapper.selectOne(qw);
            animalRescue.setTypeText(category.getName());

            Integer handleStatus = animalRescue.getHandleStatus();
            if (handleStatus == 1) {
                animalRescue.setStatusText("待处理");
            }
            if (handleStatus == 2) {
                animalRescue.setStatusText("已处理");
            }

        }

        long count = animalRescuePage.getTotal();
        AnimalRescueVo animalRescueVo = new AnimalRescueVo();
        animalRescueVo.setList(animalRescueList);
        animalRescueVo.setCount((int) count);
        return animalRescueVo;
    }

    @Override
    public AnimalRescueVo getAnimalRescueByType(Integer page, Integer limit, Integer type) {
        Page<AnimalRescueModel> page1 = new Page<>(page, limit);
        QueryWrapper<AnimalRescueModel> qwRescue = new QueryWrapper<>();
        qwRescue.eq("type", type);
        Page<AnimalRescueModel> animalRescuePage = animalRescueMapper.selectPage(page1, qwRescue);

        List<AnimalRescueModel> animalRescueList = animalRescuePage.getRecords();
        for (AnimalRescueModel animalRescue : animalRescueList) {
            Integer animalType = animalRescue.getType();
            QueryWrapper<CategoryModel> qw = new QueryWrapper<>();
            qw.eq("id", animalType);
            CategoryModel category = categoryMapper.selectOne(qw);
            animalRescue.setTypeText(category.getName());
        }

        long count = animalRescuePage.getTotal();
        AnimalRescueVo animalRescueVo = new AnimalRescueVo();
        animalRescueVo.setList(animalRescueList);
        animalRescueVo.setCount((int) count);
        return animalRescueVo;
    }

    @Override
    public AnimalRescueModel setRescueHandle(AnimalRescueModel animalRescue) {
        QueryWrapper<RescueHandleModel> qwHandle = new QueryWrapper<>();
        qwHandle.eq("rescue_id", animalRescue.getId());
        RescueHandleModel rescueHandle = rescueHandMapper.selectOne(qwHandle);
        List<String> urlList = new ArrayList<>();
        List<String> videoList = new ArrayList<>();
        String fileIdArr = rescueHandle.getFileId();
        if (StringTool.isNotEmpty(fileIdArr)) {
            String[] split = fileIdArr.split(",");
            for (String fileIds : split) {
                int fileId = Integer.parseInt(fileIds);
                RecordFileModel recordFile = recordFileMapper.selectById(fileId);
                if (recordFile != null) {
                    if (recordFile.getFileType() != null && recordFile.getFileType() == 1) {
                        urlList.add(recordFile.getUrl());
                    }
                    if (recordFile.getFileType() != null && recordFile.getFileType() == 2) {
                        videoList.add(recordFile.getUrl());
                    }
                }
            }
            rescueHandle.setUrlList(urlList);
            rescueHandle.setVideoList(videoList);
        }
        animalRescue.setRescueHandle(rescueHandle);
        return animalRescue;
    }

    @Override
    public AnimalRescueModel getAnimalRescueById(Integer id) {
        AnimalRescueModel animalRescueModel = animalRescueMapper.selectById(id);
        StringBuilder url = new StringBuilder();
        StringBuilder VideoUrl = new StringBuilder();
        if (StrUtil.isNotBlank(animalRescueModel.getFileId())) {
            String[] split = animalRescueModel.getFileId().split(",");
            for (int i1 = 0; i1 < split.length; i1++) {
                if (StrUtil.isNotBlank(split[i1])) {
                    RecordFileModel recordFileModel = recordFileMapper.selectById(split[i1]);
                    if (recordFileModel != null) {
                        if (recordFileModel.getFileType() != null && recordFileModel.getFileType() == 1) {
                            url.append(recordFileModel.getUrl()).append(",");
                        } else if (recordFileModel.getFileType() != null && recordFileModel.getFileType() == 2) {
                            VideoUrl.append(recordFileModel.getUrl()).append(",");
                        }
                    }
                }
            }
        }
        animalRescueModel.setOpenId(null);
        animalRescueModel.setUrl(url.toString());
        animalRescueModel.setVideoUrl(VideoUrl.toString());
        return animalRescueModel;
    }
}
